package com.ruoyi.cart.controller;

import com.ruoyi.cart.domain.Cart;
import com.ruoyi.cart.domain.CartDetail;
import com.ruoyi.cart.service.ICartService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.redis.RateLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 购物车Controller
 *
 * @author sqlstudent
 * @date 2025-06-12
 */
@RestController
@RequestMapping("/cart/cart")
public class CartController extends BaseController
{
    @Autowired
    private ICartService cartService;

    /**
     * 查询购物车列表
     */
    @RateLimiter(key = "cart:", time = 100, count = 50, message = "购物车操作过于频繁")
    @PreAuthorize("@ss.hasPermi('cart:cart:list')")
    @GetMapping("/list")
    public TableDataInfo list(Cart cart)
    {
        startPage();
        List<CartDetail> list = cartService.selectCartList02(cart);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('cart:cart:list')")
    @GetMapping("/listid")
    public TableDataInfo listid(Cart cart)
    {
        startPage();
        List<Cart> list = cartService.selectCartList(cart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @PreAuthorize("@ss.hasPermi('cart:cart:export')")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cart cart)
    {
        List<Cart> list = cartService.selectCartList(cart);
        ExcelUtil<Cart> util = new ExcelUtil<Cart>(Cart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('cart:cart:query')")
    @GetMapping(value = "/{cartId}")
    public AjaxResult getInfo(@PathVariable("cartId") Long cartId)
    {
        return success(cartService.selectCartByCartId(cartId));
    }

    /**
     * 新增购物车
     */
    @RateLimiter(key = "cart:",  time = 100, count = 30, message = "购物车添加过于频繁")
    @PreAuthorize("@ss.hasPermi('cart:cart:add')")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Cart cart)
    {
        return toAjax(cartService.insertCart(cart));
    }

    /**
     * 修改购物车
     */
    @RateLimiter(key = "cart:",  time = 100, count = 30, message = "购物车更改操作过于频繁")
    @PreAuthorize("@ss.hasPermi('cart:cart:edit')")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Cart cart)
    {
        return toAjax(cartService.updateCart(cart));
    }

    /**
     * 删除购物车
     */
    @RateLimiter(key = "cart:",  time = 100, count = 30, message = "购物车删除操作过于频繁")
    @PreAuthorize("@ss.hasPermi('cart:cart:remove')")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cartIds}")
    public AjaxResult remove(@PathVariable Long[] cartIds)
    {
        return toAjax(cartService.deleteCartByCartIds(cartIds));
    }
}
